/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.storage.jdbc.sqlite;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import de.fuberlin.replication.library.Subset;
import de.fuberlin.replication.library.Version;
import de.fuberlin.replication.storage.SubsetDAO;

/**
 * SQLite Subset Data Access Object (DAO).
 */
public class SQLiteSubsetDAO extends SQLiteDAO implements SubsetDAO {
	/**
	 * @param connection SQLite DB Connection
	 */
	public SQLiteSubsetDAO(Connection connection) {
		super(connection);
	}

	@Override
	public Iterable<Subset> getAll() {
		Connection conn = getConnection();
		Iterable<Subset> iterable = null;
		try {
			Statement s = conn.createStatement();
			ResultSet res = s
					.executeQuery("SELECT s.id, k.replica, k.version "
					+ "FROM subset s LEFT JOIN knowledge k ON (s.id = k.subset_id);");
			iterable = new SQLiteIterable<Subset>(new SQLiteSubsetIterator(res).getIterator());
			s.close();
			res.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return iterable;
	}

	@Override
	public Subset create() {
		Connection conn = getConnection();

		try {
			Statement s;
			s = conn.createStatement();
			s.executeUpdate("INSERT INTO subset (id) VALUES (null)");
			ResultSet res = s.getGeneratedKeys();

			Subset subset = new Subset();
			subset.setId(res.getLong(1));
			s.close();
			res.close();

			return subset;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public void save(Subset subset) {
		Connection conn = getConnection();

		PreparedStatement prep;
		try {
			prep = conn
					.prepareStatement("INSERT INTO knowledge (subset_id, replica, version) VALUES (?, ?, ?);");

			for (Version version : subset.getKnowledge()) {
				prep.setLong(1, subset.getId());
				prep.setString(2, version.getReplica());
				prep.setLong(3, version.getVersion());
				prep.addBatch();
			}

			conn.setAutoCommit(false);
			prep.executeBatch();
			prep.close();
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(Subset subset) {
		Connection conn = getConnection();
		PreparedStatement prep;

		try {
			prep = conn.prepareStatement("DELETE FROM subset WHERE id = ?");
			prep.setLong(1, subset.getId());
			prep.executeUpdate();
			prep.close();
			prep = conn.prepareStatement("DELETE FROM knowledge WHERE subset_id = ?");
			prep.setLong(1, subset.getId());
			prep.executeUpdate();
			prep.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}